package com.fare.taluo.core.domain.crawl.adapter.sub;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.Naming;
import java.util.Date;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.core.domain.crawl.CrawlTaskModel;
import com.fare.taluo.core.domain.crawl.adapter.AbstractAdapter;
import com.fare.taluo.util.DateUtils;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;


/**
 * 
 * @author zengxiangqi/13640225259
 *
 */
public class CAAirLinePdfAdaptor extends AbstractAdapter {
	private HttpClient client = null;
	public CAAirLinePdfAdaptor(CrawlTaskModel taskModel) {
		super(taskModel);
	}

	@Override
	public List<Object> paraseToVo(Object fetchObject) throws Exception {
		return null;
	}

	@Override
	public String getUrl() throws Exception {
		return "http://www.airchina.com.cn/cn/info_and_services/schedule_download.shtml";
	}

	@Override
	public Object fetch(String url) throws Exception {
		HtmlPage pageResult = null;
		WebClient webClient = null;
		HtmlPage pageIndex = null;
		HtmlForm form = null;
		HtmlElement summitbtn = null;
		HtmlRadioButtonInput countrytype = null;
		HtmlRadioButtonInput travelType = null;
		HtmlTextInput cityNameOrg = null;
		HtmlHiddenInput cityCodeOrg = null;
		HtmlTextInput cityNameDes = null;
		HtmlHiddenInput cityCodeDes = null;
		HtmlTextInput takeoffDate = null;
		HtmlSelect adultNum = null;
		HtmlTextInput returnDate = null;
		HtmlSelect childNum = null;
		HtmlSelect cabinStage = null;
		
		webClient = this.getWebClient();
		//webClient.getOptions().setCssEnabled(false);
		//webClient.getOptions().setThrowExceptionOnScriptError(false);
		
		//打开国航下载页面
		pageIndex = webClient.getPage(getUrl());
		
		return null;
	}
	

	
	private File fetchURI(String uri) throws Exception {
		HttpPost request;
		HttpEntity entity;
		byte data[];
		request = null;
		HttpResponse response = null;
		entity = null;
		InputStream is = null;
		data = null;
		String s;
		try {
			/*if (client == null)
				client = super.getHttpClient();
			client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,1000000);
		        //响应超时
			client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000000);
			request = new HttpPost(uri);
			response = client.execute(request);
			entity = response.getEntity();*/
			
			FTPClient ftpClient = new FTPClient();
			//ftpClient.get
			try {
				ftpClient.connect("ftp://webftp.airchina.com.cn");
				//ftpClient.setControlEncoding("UTF-8");
				ftpClient.login("Airchina", "AirChina03170ag");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			
			/*if (entity != null) {
				is = entity.getContent();
				return writeFile(is);
			}*/
			/*FtpConfiguration config = new FtpConfiguration();
			config.setFtpHost("202.96.17.78");
			config.setFtpPort(21);
			config.setLocaldir("d://tdfsadf.pdf");
			FTPChannel service = new FTPChannel();
			service.connect(config);
			service.downloadFilesInDir("ftp://Airchina:AirChina03170ag@webftp.airchina.com.cn/cn.pdf", "d:\\");
		    */
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (Exception e) {
				}
			;
			is = null;
			if (request != null)
				request.releaseConnection();
			if (entity != null)
				EntityUtils.consume(entity);
			entity = null;
			response = null;
			request = null;
		}
		return null;
	}
	
	/**
	 * 保存pdf
	 * @param is
	 * @return
	 * @throws IOException
	 */
	protected File writeFile(InputStream is) throws IOException {
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		int BUFFER_SIZE = 1024;
		byte[] buf = new byte[BUFFER_SIZE];
		int size = 0;
		bis = new BufferedInputStream(is); 
        //建立文件 
		File file = new File(DateUtils.dateToStr(new Date(),
				"yyyy-MM-dd-CA")+".pdf");
	    fos = new FileOutputStream(file);   
	    //保存文件   
	    while ( (size = bis.read(buf)) != -1)    
	    { fos.write(buf, 0, size); }  
	 
	    fos.close();   
	    bis.close();
		return file;
	}
	@Override
	public boolean validateFetch(Object fetchObject) throws Exception {
		return false;
	}  
	public static void main(String[] args) {
		try {
			ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
			//new CAAirLinePdfAdaptor(null).fetchURI("ftp://Airchina:AirChina03170ag@webftp.airchina.com.cn/cn.pdf");
			/*FTPService service = new FTPChannel(new FtpConfiguration());
			service.downloadFilesInDir("ftp://Airchina:AirChina03170ag@webftp.airchina.com.cn/cn.pdf", "d:\\d.pdf");*/
		
			//ftp://Airchina:AirChina03170ag@webftp.airchina.com.cn/cn.pdf
			//ftp://Airchina@webftp.airchina.com.cn
			
			
			//IProxyIPRMIService remote = (IProxyIPRMIService)Naming.lookup("rmi://10.108.1.230:1199/proxyIPService");
			// System.out.println(remote.getChannelProxyIp("1", "xxxx").getProxyIp());

			// new CAAirLinePdfAdaptor(null).fetch(null);
			 
			//ftp://Airchina:AirChina03170ag@webftp.airchina.com.cn/cn.pdf
			FTPClient ftpClient = new FTPClient();
			//ftpClient.get
			ftpClient.connect("webftp.airchina.com.cn");
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.login("Airchina", "AirChina03170ag");
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			//FTPFile[] files = ftpClient.listFiles("/");
			 
			 //
			// System.out.println(files.length);
		//	System.out.println(ftpClient.login("", ""));
			
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
